//
//  WKBleViewController.m
//  ourToken
//
//  Created by wakeup on 2018/6/8.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKBleViewController.h"
#import "WKBaseHeader.h"

@interface WKBleViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIImageView *backImageView;//背景
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CBPeripheral *peripheral;//当前连接的设备
@property (nonatomic, assign) BOOL connectStatus;//蓝牙连接状态

@end

@implementation WKBleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    [self getNetworking];
}

- (void)getNetworking {
    [httpManager bindingDevicesWithIdentifier:@"1" success:^(NSDictionary *result) {
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self setBleNotify];
    [self.tableView reloadData];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = @"连接设备";
    self.navigationView.backgroundColor = [UIColor clearColor];
    self.navigationView.topRightButton.hidden = NO;
    [self.navigationView.topRightButton setTitle:@"重新扫描" forState:UIControlStateNormal];
}

- (void)layoutUI {
    [self.view addSubview:self.backImageView];
    [self.view addSubview:self.tableView];
    [self.view bringSubviewToFront:self.navigationView];
}

#pragma mark - Notification
- (void)setBleNotify {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleBleNotify:) name:kTag_Phone_Ble_On object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleBleNotify:) name:kTag_Phone_Discover_Device object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleBleNotify:) name:kTag_Device_Connected object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleBleNotify:) name:kTag_Device_Disconnected object:nil];
}

- (void)handleBleNotify:(NSNotification *)notify {
    
    if ([notify.name isEqualToString:kTag_Phone_Ble_On]) {
        [bleSelf startFindBleDevices];
    }
    else if ([notify.name isEqualToString:kTag_Phone_Discover_Device]) {
        [self.tableView reloadData];
    }
    else if ([notify.name isEqualToString:kTag_Device_Connected]) {
        self.peripheral = [notify object];
        self.connectStatus = YES;
        bleSelf.peripheralArray.removeAllObjects;
        bleSelf.bleArray.removeAllObjects;
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if ([notify.name isEqualToString:kTag_Device_Disconnected]) {
        self.connectStatus = NO;
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return bleSelf.bleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"BleCell"];
    WKBleModel *model = bleSelf.bleArray[indexPath.row];
    cell.textLabel.text = model.advertisementData[CBAdvertisementDataLocalNameKey];
    cell.textLabel.font = AppManager.tableTextFont;
//    cell.imageView.image = [UIImage imageNamed:@"gouqing"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 15)];
    imageView.image = [UIImage imageNamed:@"gouqing"];
    cell.accessoryView = imageView;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [bleSelf stopFindBleDevices];
    WKBleModel *model = bleSelf.bleArray[indexPath.row];
    [bleSelf connectBleDevice:model.peripheral];
}

#pragma mark - 懒加载UI
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, AppManager.navigationHeight, AppManager.width, AppManager.height-AppManager.navigationHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = AppManager.tableBackColor;
//        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, 10)];
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (UIImageView *)backImageView {
    if (_backImageView == nil) {
        _backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, AppManager.height)];
        _backImageView.image = [UIImage imageNamed:@"shoukuanbg"];
    }
    return _backImageView;
}

- (void)rightButtonClick:(UIButton *)sender {
    bleSelf.peripheralArray.removeAllObjects;
    bleSelf.bleArray.removeAllObjects;
    [self.tableView reloadData];
//    if (self.connectStatus == YES) {
//        [bleSelf disconnectBleDevice:self.peripheral];
        [bleSelf startFindBleDevices];
//    }
}

@end
